package com.gec.auction.controller;

import com.gec.auction.pojo.Auction;
import com.gec.auction.pojo.AuctionCustomer;
import com.gec.auction.pojo.Auctionrecord;
import com.gec.auction.pojo.User;
import com.gec.auction.service.AuctionCustomerService;
import com.gec.auction.service.AuctionService;
import com.gec.auction.tools.AuctionCustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Irene_baebae
 * @version 1.0
 */
@Controller

public class AuctionController {
    /**
     * 查询所有拍卖品方法
     * @param condition
     * @return
     */
    @Autowired
    AuctionService auctionService;

    @Autowired
    AuctionCustomerService auctionCustomerService;
//    @RequestMapping({"/list"})
//    public ModelAndView list(Auction condition){
//        ModelAndView modelAndView = new ModelAndView();
//        List<Auction> auctionList = auctionService.getAuction(condition);
//        modelAndView.addObject("auctionList",auctionList);
//        //将查询条件数据也放回去 做数据回显
//        modelAndView.addObject("condition",condition);
//        modelAndView.setViewName("index");
//        return modelAndView;
//    }

    private static final int pageSize=8;
    /*
    查询所有拍卖商品的方法 跟条件查询是结合到一起来的
     */

    @RequestMapping("/list")
    public ModelAndView list(@ModelAttribute("condition")Auction condition, @RequestParam(name = "pageNum",required = false,defaultValue = "1" )int pageNum, String auctionname, String auctiondesc, BigDecimal auctionstartprice){
        //默认去数据库中查询 所有的 拍卖品信息 然后用于展示到页面中
        // ModeTAndView 模型视图对象 就是用来携带数据 和页面模版的对象
        // springMVC框架 就会根据 模型视图对象 将数据和模版页面染成最终展示给浏览器的html页面

        ModelAndView modelAndView = new ModelAndView();
        //就是只需要将要显示的数据 和页面 展示出来即可
        //调用业务逻辑代码 查询出数据先
        PageHelper.startPage(pageNum,pageSize);
        List<Auction> auctionList = auctionService.getAuction(condition,auctionname,auctiondesc,auctionstartprice);
        //将查询出来的数据设置到模型视图对象中
        //因为页面采用<ul th:each="auction,state:$fauctionLis”auctionList来获取数据 所以这
        //注意检查单词 不要敲错了
        modelAndView.addObject("auctionList",auctionList);
        PageInfo page=new PageInfo(auctionList);
        modelAndView.addObject("page",page);
        //将查询条件数据也放回去 做数据的回显
        modelAndView.addObject("condition",condition);
        //设置要显示的模版页面index.html 不用写html
        modelAndView.setViewName("index");
        //return 之后 springMVC框架就会将数据放到模版页面index.html中 变成最终要显示的页面
        return modelAndView;
    }

//    @RequestMapping("/getAuctionById")
//    public ModelAndView getAuctionById(int auctionId){
//        ModelAndView modelAndView = new ModelAndView();
//        Auction auction = auctionService.getAuctionById(auctionId);
//        modelAndView.addObject("auctionDetail",auction);
//        modelAndView.setViewName("auctionDetail");
//        return modelAndView;
//    }
//编写根据商品编号 去获取对应的商品信息
@RequestMapping ("/getAuctionById")
public ModelAndView getAuctionById( int auctionId){
    ModelAndView modelAndView = new ModelAndView();
    //去调用业务逻辑代码查询数据
    Auction auction = auctionService.getAuctionById(auctionId);
    //获取业务逻辑代码的数据
    modelAndView.addObject("auctionDetail",auction);
    //设置视图的名称
    modelAndView.setViewName("auctionDetail");
    return modelAndView;
}

    @RequestMapping("/addAuction")
    public String Auction(){
        return "addAuction";
    }

    @RequestMapping("/publishAuctions")
    public String publishAuctions(Auction auction, MultipartFile pic) throws IOException {
        System.out.println(auction);
        String originalFilename = pic.getOriginalFilename();
        String contentType = pic.getContentType();
        //防止重命名
        String newFileName = UUID.randomUUID().toString();
        String s = originalFilename.split("\\.")[1];
        String finalName = newFileName + "." + s;

        String folder = "D:\\study\\auction\\auction\\src\\main\\java\\com\\gec\\auction\\upload";

        //创建文件对象 用来接收图片
        File file = new File(folder, finalName);
        pic.transferTo(file);

        auction.setAuctionpictype(contentType);
        auction.setAuctionpic("http://localhost:8888/upload/"+finalName);

        auctionService.addAuction(auction);
        //提交成功应该是返回到首页 redirect重定向返回到首页
        return "redirect:list";
    }

    @RequestMapping ("/deleteByPrimaryKey")
    public String deleteByPrimaryKey( Long auctionId){
        auctionService.deleteByPrimaryKey(auctionId);
        return "succeed";
    }

    //根据拍卖商品编号查询该商品详情信息
    @RequestMapping(value = "getAuctionCustomerById")
    public String getAuctionDetail(Integer auctionId, Model model){
        Auction auction = auctionCustomerService.getAuctionByActionId(auctionId); //从数据库得到拍卖商品详情信息
        model.addAttribute("auctionDetail",auction);  //存储到域对象当中
        return "auctionDetail";
    }


    @PostMapping("/saveAuctionRecord")
    public String saveAuctionRecord(Auctionrecord record, Model model, HttpSession session) throws AuctionCustomerException {
        //获取当前登录用户名信息
        User user = (User) session.getAttribute("user");
        record.setUserid(user.getUserid());
        //获取当前时间
        record.setAuctiontime(new Date());
        //执行添加竞拍记录
        auctionService.addAuctionRecored(record);
        //添加记录成功后刷新当前页面内容
        return "redirect:/getAuctionCustomerById?auctionId="+record.getAuctionid();
    }


    @RequestMapping(value = "/toAuctionResult")
    public String toAuctionResult(Model model){
        List<AuctionCustomer> list = auctionService.selectAuctionEndTime(); //结束活动的商品
//        List<Auction> noEndList = auctionService.selectAuctionNoEndTime(); //正在拍卖的商品
        model.addAttribute("endtimeList",list);
//        model.addAttribute("noendtimeList",noEndList);
        return "auctionResult";
    }
}
